package cn.how2go.data.component;

import cn.how2go.common.vo.Expand;
import cn.how2go.common.vo.JT0200;
import cn.how2go.data.config.GPSTransToAMap;
import cn.how2go.data.entity.Cone;
import cn.how2go.data.entity.Wub;
import cn.how2go.data.service.*;
import cn.how2go.data.util.BinaryUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/7/15.
 */
@Component
@RabbitListener(queues = RabbitmqQueue.LOCATION_QUEUE)
public class RabbitmqReceive {

    @Autowired
    private LocationService locationService;

    @Autowired
    private LocationHisService locationHisService;

    @Autowired
    private WarnningService warnningService;

    @Autowired
    private StatusService statusService;

    @Autowired
    private CarsService carsService;

    @Value("${api.url}")
    private String apiurl;

    @RabbitHandler
    public void process(JT0200 bean) {
        System.out.println("receive\r\n"+bean.toString());
        Cone param = new Cone();
        Wub wub = new Wub();
        String simno = bean.getSimno();
        int lingNo = 12-simno.length();
        String ling = "";
        for(int i=0;i<lingNo;i++){
            ling += "0";
        }
        simno = ling+simno;
        param.setTermID(simno);
        param.setLon(bean.getLocation().getLongitude());
        param.setLat(bean.getLocation().getLatitude());
        //获取扩展信息
        Expand expand = bean.getExpand();
        byte[] expandExt = expand.getBytes();

        //获取高精度定位扩展信息E1
        ByteBuf directBuf = Unpooled.directBuffer(16);
        directBuf.writeBytes(expandExt);
        int lens = expandExt.length;
        int flag = 0;
        while(flag<= lens){
            byte type = directBuf.readByte();
            flag++;
            byte len = directBuf.readByte();
            flag++;
            flag = flag + len;
            //如果读取扩展信息为0xE1,就是所需要的信息
            if(((byte)0xE1) == type){
                if(len == 0x0a) {
                    //版本号
                    wub.setVersion(directBuf.readByte());
                    //状态
                    wub.setStatus(directBuf.readByte());
                    //高精度经纬度
                    wub.setBcdlng(Integer.parseInt(BinaryUtil.bytes2HexString(new byte[]{directBuf.readByte()})));
                    wub.setBcdlat(Integer.parseInt(BinaryUtil.bytes2HexString(new byte[]{directBuf.readByte()})));
                    //fhdop,fvdop,fpdop
                    wub.setFhdop(directBuf.readShort());
                    wub.setFvdop(directBuf.readShort());
                    wub.setFpdop(directBuf.readShort());
                }
                //获取完数据不再循环
                break;
            }else{
                //不是E1读完不进行解析
                byte[] msg = new byte[len];
                directBuf.readBytes(msg);
            }
        }
        param.setStatus(wub.getStatus());
        if(wub.getStatus()!=0x00 && wub.getStatus() != 0x09) {
            if(wub.getStatus()==0x04 || wub.getStatus()==0x05){
                param.setAlt(bean.getLocation().getAltitude());
            }
            double[] lnglat = GPSTransToAMap.transform((bean.getLocation().getLatitude()*Math.pow(10,8)+wub.getBcdlat())/Math.pow(10,8),
                    (bean.getLocation().getLongitude()*Math.pow(10,8)+wub.getBcdlng())/Math.pow(10,8));
            param.setLon2(lnglat[1]);
            param.setLat2(lnglat[0]);
            //原始坐标也要变换
            param.setLat((bean.getLocation().getLatitude()*Math.pow(10,8)+wub.getBcdlat())/Math.pow(10,8));
            param.setLon((bean.getLocation().getLongitude()*Math.pow(10,8)+wub.getBcdlng())/Math.pow(10,8));
            System.out.println(param.toString());
            String result2 = HttpRequest.post(apiurl)
                    .body(JSONUtil.toJsonStr(param))
                    .execute().body();
            System.out.println(result2);
        }
    }
}
